<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
            * 最不利链路：
            * 最不利链路寻找规则：根据次数和时长两个值分别从顶节点开始逐级寻找
            * 次数：
            * 每级找最大值，以此为始继续往下级寻找，直到叶子节点停止，
            * 若该级存在多个相同最大值节点，则同时从这些节点向下寻找
            * 时长：
            * 每级找最大值，以此为始继续往下级寻找，直到叶子节点停止，
            * 若该级存在多个相同最大值节点，则同时从这些节点向下寻找
            * 默认最多查找3条最不利链路：即基于当前树结果找3次最不利链路，第二次寻找最不利链路则是把第一次寻找的最不利链路屏蔽掉继续找，后续以此类推
            */
        let tree = {
            name: '5，6',
            num: 5,
            duration: 6,
            children: [
                {
                    name: '10，3',
                    parentId: '1',
                    num: 10,
                    duration: 3,
                    children: [
                        {
                            name: '12， 9',
                            parentId: '1-1',
                            num: 12,
                            duration: 9
                        },
                        {
                            name: '13， 8',
                            parentId: '1-1',
                            num: 13,
                            duration: 8
                        }
                    ]
                },
                {
                    name: '5， 6',
                    parentId: '1',
                    num: 5,
                    duration: 6,
                    children: [
                        {
                            name: '6， 3',
                            parentId: '1-2',
                            num: 6,
                            duration: 3
                        },
                        {
                            name: '8， 5',
                            parentId: '1-2',
                            num: 8,
                            duration: 5
                        }
                    ]
                },
                {
                    name: '10， 2',
                    parentId: '1',
                    num: 10,
                    duration: 2
                }
            ]
        }
        const onSearchLink = (tree, keys) => {
            const treeCopy = JSON.parse(JSON.stringify(tree))
            function deep(data, keyList = ['num', 'duration'], type = 'target') {
                if (data.length === 0) return []
                let map = {}
                keyList.forEach(k => {
                    function fn(a, b) {
                        return b[k] - a[k];
                    }
                    let list = JSON.parse(JSON.stringify(data))
                    list.sort(fn)
                    let firstItem = list[0]
                    if (type === 'target') {
                        map[k] = list.filter(x => x[k] === firstItem[k])
                    }
                    if (type === 'residue') {
                        map[k] = list.filter(x => x[k] !== firstItem[k])
                    }
                    map[k].forEach(item => {
                        if (item.children) {
                            item.children = deep(item.children, [k])
                        }
                    })
                })
                const res = Object.values(map).flat(Infinity)
                return res
            }
            const treeTarget = {
                ...tree,
                children: deep(treeCopy.children, keys, 'target')
            }
            const treeResidue = {
                ...tree,
                children: deep(treeCopy.children, keys, 'residue')
            }
            return {
                treeTarget,
                treeResidue
            }
        }
        const resNum = onSearchLink(tree, ['num', 'duration'])
        console.log(tree)
        console.log(resNum)
    </script>
</body>

</html>